
/*
PIC32MX795F512L 3.2" TFT LCD Demo Board USB Host Support
Copyright (C) 2012 Craig Dawson, Aaron Mohtar, Craig Peacock

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "GenericTypeDefs.h"
#include "HardwareProfile.h"
#include "USB/usb.h"
#include "USB/usb_host_msd.h"
#include "USB/usb_host_msd_scsi.h"

// Media Interface Function Pointer Table for the Mass Storage client driver
CLIENT_DRIVER_TABLE usbMediaInterfaceTable =
{
    USBHostMSDSCSIInitialize,
    USBHostMSDSCSIEventHandler,
    0
};

// Client Driver Function Pointer Table for the USB Embedded Host foundation
CLIENT_DRIVER_TABLE usbClientDrvTable[] =
{
    {
        USBHostMSDInitialize,
        USBHostMSDEventHandler,
        0
    }
};

// USB Embedded Host Targeted Peripheral List (TPL)
USB_TPL usbTPL[] =
{
    { INIT_CL_SC_P( 8ul, 6ul, 0x50ul ), 0, 0, {TPL_CLASS_DRV} } // Most types of MSD flash drives
    ,
    { INIT_CL_SC_P( 8ul, 5ul, 0x50ul ), 0, 0, {TPL_CLASS_DRV} } // Some MSD flash drives use this instead
};

void TMR3Init(void)
{
    T3CONbits.TCKPS = 0b111;    // 80MHz / 1:256 Prescaler = 3.2uS tick
    PR3 = 31300;                // ~100mS

    IPC3bits.T3IP = 7;          // Priority 3
    IPC3bits.T3IS = 3;          // Sub-priority
    IFS0bits.T3IF = 0;          // Clear TMR3 flag;
    IEC0bits.T3IE = 1;          // Turn on TMR3 interrupt

    T3CONbits.ON = 1;           // Turn on TMR3
}

void __attribute((interrupt(ipl7), vector(_TIMER_3_VECTOR), nomips16)) _Timer1Handler(void)
{
    USBTasks();
    IFS0bits.T3IF = 0;    // Clear TMR3 flag;
}

BOOL USB_ApplicationEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size )
{
    switch( event )
    {
        case EVENT_VBUS_REQUEST_POWER:
            printf("EVENT_VBUS_REQUEST_POWER\r\n");
            return TRUE;

        case EVENT_VBUS_RELEASE_POWER:
            printf("EVENT_VBUS_REQUEST_POWER\r\n");
            return TRUE;
            break;

        case EVENT_HUB_ATTACH:
            return TRUE;
            break;

        case EVENT_UNSUPPORTED_DEVICE:
            printf("EVENT_UNSUPPORTED_DEVICE\r\n");
            return TRUE;
            break;

        case EVENT_CANNOT_ENUMERATE:
            return TRUE;
            break;

        case EVENT_CLIENT_INIT_ERROR:
            return TRUE;
            break;

        case EVENT_OUT_OF_MEMORY:
            return TRUE;
            break;

        case EVENT_UNSPECIFIED_ERROR:  
            return TRUE;
            break;

        default:
            printf("USB Event %d\r\n",event);
            break;
    }

    return FALSE;
}


 